﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DECOR.BL.BE;
using DECOR.DL.DALC.SAPC.Proxy;
using System.Data;

namespace DECOR.DL.DALC.SAPC
{
    public class JerarquiaDALC : IJerarquiaDALC
    {
        public static JerarquiaDALC Instance()
        {
            if (_instance == null)
            {
                lock (typeof(JerarquiaDALC))
                {
                    if (_instance == null)
                    {
                        _instance = new JerarquiaDALC();
                    }
                }
            }
            return _instance;
        }

        private JerarquiaDALC() { }
        private static volatile JerarquiaDALC _instance = null;


        #region IJerarquiaDALC Members

        public List<JerarquiaBE> listar(int pNivel)
        {
            JerarquiasProxy proxy = new JerarquiasProxy();
            proxy.Connection = DALC.getConnection;
            if (!proxy.Connection.IsOpen) { proxy.Connection.Open(); }
            /* llamando a la función */
            V_T179Table tblResult = new V_T179Table();

            proxy.Zweb_Lista_Jerarquias(pNivel.ToString(), ref tblResult);

            SAP.Connector.SAPConnectionPool.ReturnConnection(proxy.Connection);
            proxy.Connection = null;
            /* Manejando el retorno */
            DataTable dt = tblResult.ToADODataTable();

            List<JerarquiaBE> listaResult = new List<JerarquiaBE>();
            foreach (DataRow dr in dt.Rows)
            {
                JerarquiaBE obj = new JerarquiaBE();
                obj.Jerarquia = dr["PRODH"].ToString();
                obj.Descripcion = dr["VTEXT"].ToString();
                obj.Nivel = Convert.ToInt32(dr["STUFE"].ToString());
                listaResult.Add(obj);
            }
            return listaResult;
        }

        public List<JerarquiaBE> Buscar(int pNivel, string pJerarquia)
        {
            JerarquiasProxy proxy = new JerarquiasProxy();
            proxy.Connection = DALC.getConnection;
            if (!proxy.Connection.IsOpen) { proxy.Connection.Open(); }
            /* llamando a la función */
            V_T179Table tblResult = new V_T179Table();

            proxy.Zweb_Busca_Jerarquias(pJerarquia, pNivel.ToString(), ref tblResult);

            SAP.Connector.SAPConnectionPool.ReturnConnection(proxy.Connection);
            proxy.Connection = null;
            /* Manejando el retorno */
            DataTable dt = tblResult.ToADODataTable();

            List<JerarquiaBE> listaResult = new List<JerarquiaBE>();
            foreach (DataRow dr in dt.Rows)
            {
                JerarquiaBE obj = new JerarquiaBE();
                obj.Jerarquia = dr["PRODH"].ToString();
                obj.Descripcion = dr["VTEXT"].ToString();
                obj.Nivel = Convert.ToInt32(dr["STUFE"].ToString());
                listaResult.Add(obj);
            }
            return listaResult;
        }

        #endregion
    }
}
